package com.itheima.demo4;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        //名称           重量(升)	   总价值
        //水		       4      	   24
        //牛奶  		  8      	  160
        //五粮液		 2    	     4000
        //可乐		  6     	  108
        //茅台		  1    	      4000
        Item[] items = {
                new Item("水", 4, 24),
                new Item("牛奶", 8, 160),
                new Item("五粮液", 2, 4000),
                new Item("可乐", 6, 108),
                new Item("茅台", 1, 4000)
        };
        Arrays.sort(items, (o1, o2) -> o2.getPrice() / o2.getWeight() - o1.getPrice() / o1.getWeight());
        int maxPrice = 0;
        int maxWeight = 10;
        for (int i = 0; i < items.length; i++){
            if(maxWeight == 0) break;
            if (maxWeight >= items[i].getWeight()){
                System.out.println("取" + items[i].getName() + items[i].getWeight() + "升");
                maxPrice += items[i].getPrice();
                maxWeight -= items[i].getWeight();
            }
            else {
                System.out.println("取" + items[i].getName() + maxWeight + "升");
                maxPrice += items[i].getPrice() * maxWeight / items[i].getWeight();
                maxWeight = 0;
            }
        }
        System.out.println("当前总价值：" + maxPrice);
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Item{
    private String name;
    private int weight;
    private int price;
}